//
//  SSCMoreGameManagerAdapter.m
//  Unity-iPhone
//
//  Created by xuzepei on 16/1/19.
//
//

#import "SSCMoreGameManagerAdapter.h"
#if __has_include(<SSCAdsSDK/SSCAdsSDK.h>)
#import <SSCAdsSDK/SSCAdsSDK.h>
#else
#import "AdIdHelper.h"
#endif


@implementation SSCMoreGameManagerAdapter

+ (SSCMoreGameManagerAdapter*)getInstance
{
    static SSCMoreGameManagerAdapter* sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token, ^{
        sharedInstance = [[SSCMoreGameManagerAdapter alloc] init];
        [AboutUsPage aboutUsPageWithDelegate:sharedInstance];
    });
    
    return sharedInstance;
}

- (void)setGameObjectName:(char*)name
{
    if(name && strlen(name))
    {
        self.objectName = [NSString stringWithFormat:@"%s",name];
    }
}

- (char*)getIconsPath
{
    NSMutableString* temp = [[NSMutableString alloc] init];
    NSArray* array = [AboutUsPage getIconsPath];
    for(NSString* str in array)
    {
        [temp appendFormat:@"%@,",str];
    }

    char *p = new char[[temp length]+1];
    return strcpy(p, temp.UTF8String);
}

- (char*)getButtonImagePath
{
    NSString* temp = [[AboutUsPage getInstance] getButtonImagePath];
    char *p = new char[[temp length]+1];
    return strcpy(p, temp.UTF8String);
}

- (char*)getDefaultAppIconPath
{
    NSString* temp = [[AboutUsPage getInstance] getDefaultAppIconPath];
    char *p = new char[[temp length]+1];
    return strcpy(p, temp.UTF8String);
}

- (char*)getFirstAppIconPath
{
    NSString* temp = [[AboutUsPage getInstance] getFirstAppIconPath];
    char *p = new char[[temp length]+1];
    return strcpy(p, temp.UTF8String);
}

- (char*)getSmallButtonImagePath
{
    NSString* temp = [[AboutUsPage getInstance] getSmallButtonImagePath];
    char *p = new char[[temp length]+1];
    return strcpy(p, temp.UTF8String);
}

- (void)refresh
{
    [[AboutUsPage getInstance] requestAboutUsPageInfo];
}

- (void)show:(int)animation
{
    [[AboutUsPage getInstance] showWithanimation:animation params:nil];
}

#pragma mark - AboutUsPageDelegate

- (void)downloadImagesFinished
{
    static NSString *methodName = @"OnMoreGameDownloadImagesFinished";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

- (void)pageDidAppear:(AboutUsPage *)aboutusPage
{
    static NSString *methodName = @"OnMoreGameDidAppear";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

- (void)pageDidDisappear:(AboutUsPage *)aboutusPage
{
    static NSString *methodName = @"OnMoreGameDidDisappear";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

@end

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
    
    //To set the name of game object for UnitySendMessage method
    void _setGameObjectNameForMGM(char* gameObjectName)
    {
        [[SSCMoreGameManagerAdapter getInstance] setGameObjectName:gameObjectName];
    }
    
    char* _getIconsPath()
    {
        return [[SSCMoreGameManagerAdapter getInstance] getIconsPath];
    }
    
    char* _getButtonImagePath()
    {
        return [[SSCMoreGameManagerAdapter getInstance] getButtonImagePath];
    }
    
    char* _getDefaultAppIconPath()
    {
        return [[SSCMoreGameManagerAdapter getInstance] getDefaultAppIconPath];
    }
    
    char* _getFirstAppIconPath()
    {
        return [[SSCMoreGameManagerAdapter getInstance] getFirstAppIconPath];
    }
    
    char* _getSmallButtonImagePath()
    {
        return [[SSCMoreGameManagerAdapter getInstance] getSmallButtonImagePath];
    }
    
    void _refresh()
    {
        [[SSCMoreGameManagerAdapter getInstance] refresh];
    }
    
    void _show(int animation)
    {
        [[SSCMoreGameManagerAdapter getInstance] show:animation];
    }
    
}
